Page({
    data: {
        id: 0,
        isLike: true,
        // detailInfo: {
        //     //商品名称
        //     name: "食用艾草叶",
        //     //商品价格
        //     price: "88元/箱",
        //     //商品banner图
        //     imgUrls: [
        //         "../../static/goods/goods1.png",
        //         "../../static/goods/goods2.png",
        //         "../../static/goods/goods3.png",
        //     ],
        //     //商品详情图
        //     detailImg: [
        //         "../../static/goods/goods2.png",
        //         "../../static/goods/goods2.png",
        //     ],
        //     //商品描述
        //     description: "理疗效果很好,理疗效果很好理疗效果很好理疗效果很好理疗效果很好理疗效果很好理疗效果很好",
        //     //销售量
        //     sale_num: 200
        // },
        detailInfo: {},
        indicatorDots: true, //是否显示面板指示点
        autoplay: true, //是否自动切换
        interval: 3000, //自动切换时间间隔,3s
        //duration: 1000,   滑动动画时长1s


        select: 1,//tab默认选中第一个
        detailSelect: 1,//商品详情tab默认选中第一个
        tabs: [
            {
                id: 1,
                name: "评论"
            },
            {
                id: 2,
                name: "推荐"
            },
            {
                id: 3,
                name: "详情"
            }
        ],
        commentList: [
            {
                id: 1,
                avatar: "//img12.360buyimg.com/babel/s340x420_jfs/t20371/32/1913614468/53754/b55c71b2/5b3c4049N3ceedd04.jpg!q90!cc_340x420",
                nickname: "张三",
                comment: "刚送到就迫不及待地打开试了下，外观包装挺漂亮的，用起来也很舒服，加上水用着还行，不知再热点加冰效果如何。整体来说还行，用一段时间再点评。"
            },
            {
                id: 2,
                avatar: "//img12.360buyimg.com/babel/s340x420_jfs/t20371/32/1913614468/53754/b55c71b2/5b3c4049N3ceedd04.jpg!q90!cc_340x420",
                nickname: "李四",
                comment: "刚送到就迫不及待地打开试了下，外观挺漂亮的，吹出的风比较温和，今天的室内温度有30度，加上水用着还行，不知再热点加冰效果如何。整体来说还行，用一段时间再点评。"
            },
            {
                id: 3,
                avatar: "//img12.360buyimg.com/babel/s340x420_jfs/t20371/32/1913614468/53754/b55c71b2/5b3c4049N3ceedd04.jpg!q90!cc_340x420",
                nickname: "王五",
                comment: "刚送到就迫不及待地打开试了下，外观挺漂亮的，吹出的风比较温和，今天的室内温度有30度，加上水用着还行，不知再热点加冰效果如何。整体来说还行，用一段时间再点评。"
            },
            {
                id: 4,
                avatar: "//img12.360buyimg.com/babel/s340x420_jfs/t20371/32/1913614468/53754/b55c71b2/5b3c4049N3ceedd04.jpg!q90!cc_340x420",
                nickname: "赵六",
                comment: "刚送到就迫不及待地打开试了下，外观挺漂亮的，吹出的风比较温和，今天的室内温度有30度，加上水用着还行，不知再热点加冰效果如何。整体来说还行，用一段时间再点评。"
            }
        ],
        recommendList: [
            {
                id: 1,
                title: "食用艾草叶",
                img: "../../static/goods/goods1.png",
                price: "399",
                originalPrice: "488"
            },
            {
                id: 2,
                title: "食用艾草叶",
                img: "../../static/goods/goods2.png",
                price: "399",
                originalPrice: "488"
            },
            {
                id: 3,
                title: "食用艾草叶",
                img: "../../static/goods/goods3.png",
                price: "399",
                originalPrice: "488"
            },
            {
                id: 4,
                title: "食用艾草叶",
                img: "../../static/goods/goods4.png",
                price: "399",
                originalPrice: "488"
            },
            {
                id: 5,
                title: "食用艾草叶",
                img: "../../static/goods/goods5.png",
                price: "399",
                originalPrice: "488"
            },
            {
                id: 6,
                title: "食用艾草叶",
                img: "../../static/goods/goods7.png",
                price: "399",
                originalPrice: "488"
            }
        ],
        detailTabs: [
            {
                id: 1,
                name: "商品介绍"
            },
            {
                id: 2,
                name: "咨询与售后"
            }
        ]
    },
    onLoad: function () {
        // console.log(wx.getStorageSync('goods_obj'));  
        let obj = wx.getStorageSync('goods_obj')
        this.setData({
            detailInfo: obj, //把传过来的商品赋值给当前的商品对象
            // id: Math.floor(Math.random() * 10000)
            id: obj.id
        })

    },
    //预览图片
    previewImage: function (e) {
        var current = e.target.dataset.src;

        wx.previewImage({
            current: current, // 当前显示图片的http链接  
            urls: this.data.imgUrls // 需要预览的图片http链接列表  
        })
    },
    // 收藏
    addLike() {
        this.setData({
            isLike: !this.data.isLike
        });
    },
    // 跳到购物车
    toCar() {
        wx.switchTab({
            url: '/pages/cart/cart'
        })
    },
    //添加购物车
    handleCartAdd() {
        let obj = {}
        let temp = wx.getStorageSync('goods_obj')
        obj.goods_id = this.data.id
        obj.goods_small_logo = temp.imgUrls[0]
        obj.goods_name = temp.name
        obj.goods_price = temp.price

        let cart = wx.getStorageSync("cart") || [];
        let index = cart.findIndex(v => v.goods_id === obj.goods_id)
        if (index === -1) {
            obj.num = 1
            obj.checked = true
            cart.push(obj)
        } else {
            cart[index].num++
        }
        wx.setStorageSync('cart', cart)
        wx.showToast({
            title: '已经加入购物车',
            icon: 'success',
            // true 防止用户 手抖 疯狂点击按钮 
            mask: true
        })
    },
    // 立即购买
    immeBuy() {
        wx.showToast({
            title: '购买成功',
            icon: 'success',
            duration: 2000
        });
    },
    /*点击tab切换*/
    selectTab: function (event) {
        this.setData({
            select: event.currentTarget.id
        })
    },
    /*商品详情tab切换*/
    selectDetailTab: function (event) {
        this.setData({
            detailSelect: event.currentTarget.id
        })
    }
})